package services;

import dao.CreateReport_Repository;
import models.CreateReport_P_Entity;
import models.DatabaseExecutionContext;
import models.DetailList_F_Entity;
import models.Fhrb_T_Entity;
import play.db.jpa.JPAApi;

import javax.inject.Inject;
import java.util.List;
import java.util.concurrent.CompletionStage;

import static java.util.concurrent.CompletableFuture.supplyAsync;


/**
 * Provide JPA operations running inside of a thread pool sized to the connection pool
 */
public class CreateReport_Repository_JPA implements CreateReport_Repository {

    private final JPAApi jpaApi;
    private final DatabaseExecutionContext executionContext;

    @Inject
    public CreateReport_Repository_JPA(JPAApi jpaApi, DatabaseExecutionContext executionContext) {
        this.jpaApi = jpaApi;
        this.executionContext = executionContext;
    }
    
    @SuppressWarnings("unchecked")
	@Override
    public CompletionStage<List<CreateReport_P_Entity>> ReportByUserMonth(String PhoneNumber, String StartDate)
    {
    	return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
            	// 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
            	String strSql = "exec p_CreateReport @PhoneNumber = '" + PhoneNumber + "', @StartDate = '"+ StartDate +"' ";
            	return entityManager.createNativeQuery(strSql, CreateReport_P_Entity.class).getResultList();
            });
        }, executionContext);    	
    }
    
    @SuppressWarnings("unchecked")
	@Override
    public CompletionStage<List<CreateReport_P_Entity>> ReportByProduct(String PhoneNumber, String HWMC)
    {
    	return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
            	// 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
            	String strSql = "exec p_CreateReport @PhoneNumber = '" + PhoneNumber + "', @HWMC = '"+ HWMC +"' ";
            	return entityManager.createNativeQuery(strSql, CreateReport_P_Entity.class).getResultList();
            });
        }, executionContext);    	
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public CompletionStage<List<CreateReport_P_Entity>> ReportByMonthAll(String StartDate)
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                String strSql = "exec p_CreateReport @StartDate = '" + StartDate + "' ";
                return entityManager.createNativeQuery(strSql, CreateReport_P_Entity.class).getResultList();
            });
        }, executionContext);
    }

    @SuppressWarnings("unchecked")
    @Override
    public CompletionStage<List<CreateReport_P_Entity>> CreateReport(String PhoneNumber,String StartDate, String EndDate, String HWMC, String KHMC, String MonthFlag)
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                String strSql = "exec p_CreateReport @PhoneNumber = '"+PhoneNumber+"',@StartDate = '" + StartDate + "',@EndDate = '"+ EndDate+"',@HWMC = '"+HWMC+"',@KHMC ='"+KHMC+"',@MonthFlag ='"+MonthFlag+"' ";
                return entityManager.createNativeQuery(strSql, CreateReport_P_Entity.class).getResultList();
            });
        }, executionContext);
    }

    public CompletionStage<List<Fhrb_T_Entity>> StaticReport(String PhoneNumber, String StartDate, String EndDate, String HWMC, String KHMC, String MonthFlag)
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                String strSql = "select * from t_fhrb where fd_hwmc = '"+HWMC+"' ";
                return entityManager.createNativeQuery(strSql, Fhrb_T_Entity.class).getResultList();
            });
        }, executionContext);
    }

    @Override
    public CompletionStage<List<DetailList_F_Entity>> getDetailList(String PhoneNumber, String StartDate, String EndDate, String ProductType, String ProductName, String CompanyName, String unloadPlace,String mileageDef, String plateNo) {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                String strSql = "select * from f_DetailList ('"+PhoneNumber+"','"+StartDate+"','"+EndDate+"','"+ProductType+"','"+ProductName+"','"+CompanyName+"','"+unloadPlace+"','"+mileageDef+"','"+plateNo+"')";
                return entityManager.createNativeQuery(strSql, DetailList_F_Entity.class).getResultList();
            });
        }, executionContext);
    }
}